package com.example.demo.java8.functionalinterface;

import com.example.demo.java8.model.Emp;
import com.example.demo.java8.model.EmpVo;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * JAVA8 Function接口以及同类型的特化的接口
 *
 * @author luowj12
 * @since 2021/5/10 9:59
 */
public class FunctionTest {

    public static void main(String[] args) {
        /*Function<Integer, Integer> function1 = x -> x * 2;
        System.out.println(function1.apply(4));// 8

        Function<Integer, String> function2 = x -> x * 2 + "dd";
        System.out.println(function2.apply(4));//8dd

        Function<String, String> strFunction1 = (str) -> new String(str);
        System.out.println(strFunction1.apply("aa"));//aa

        Function<String, String> strFunction2 = String::new;
        System.out.println(strFunction2.apply("bb"));//bb

        Function<String, Emp> objFunction1 = (str) -> new Emp(str);
        System.out.println(objFunction1.apply("cc"));//cc

        Function<String, Emp> objFunction2 = Emp::new;
        System.out.println(objFunction2.apply("dd").getName());//dd*/

        List<Emp> list = new ArrayList<>();
        list.add(new Emp("aa"));
        List<EmpVo> listVo  = convert(v->{EmpVo empVo = new EmpVo(v.getName()); return empVo;},list);
        listVo.get(0);

    }


    public static  <R> List<R>  convert(Function<Emp, ? extends R> mapper,List<Emp> list){

        List<R> rList = list.stream().map(mapper).collect(Collectors.toList());

        return rList;
    }

}
